#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\res\vpn.ico
#AutoIt3Wrapper_Outfile=out\init-wgclient.exe
#AutoIt3Wrapper_Res_Comment=FreeSoft
#AutoIt3Wrapper_Res_Fileversion=0.1.0.8
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_CompanyName=altas
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.16.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------
#include <MsgBoxConstants.au3>
#include <AltasConstants.au3>

; set user passwd
RunWait("net user eric eric", @TempDir, @SW_HIDE)

Global Const $WG_MSI_FILE = "wireguard-amd64-0.5.3.msi"
Global Const $WG_CONF_FILE = "guest-12.zip"

Global Const $WG_GUI_FILE = "C:\Program Files\WireGuard\wireguard.exe"
Global Const $WG_FILE = "C:\Program Files\WireGuard\wg.exe"

Global Const $WORKDIR = @TempDir & "\wg_tmp"

Select
	; 检查wg并安装
	Case WG_Check()   = 0
		TrayTip ("title", "CheckWG and Exit." & @CRLF, 1)
	; 下载wg配置文件
	Case WG_GetConf() = 0
		TrayTip ("title", "ConfigWG Faild." & @CRLF, 1)
	Case WG_ImportConf() = 0
		TrayTip ("title", "InstallWG Faild." & @CRLF, 1)
	Case Else ; If nothing matches then execute the following.
		WG_Clean();
		MsgBox($MB_SYSTEMMODAL, "", "End of WG.", 3)
EndSelect
#cs ----------------------------------------------------------------------------
Install File
#ce ----------------------------------------------------------------------------
Func WG_ImportConf()
	;ShellExecute($WORKDIR)
	;Run("wireguard-amd64-0.5.3.msi")
	ShellExecuteWait($WG_GUI_FILE)
	;Run("VC_redist.x86.exe")
	;WinActive("WireGuard")
	Local $aControl = WinWaitActive("WireGuard", "从文件导入隧道", 30)

	Send("^o")

	WinWaitActive("从文件导入隧道", "配置文件", 0)
	;Send("#{SPACE}");ENG输入法
	;Send(@ScriptDir & "\ethernet1.zip")
	ControlSend ( "从文件导入隧道", "", "[CLASS:Edit; INSTANCE:1]", $WORKDIR & "\" &$WG_CONF_FILE)

	Send("{TAB}")

	Send("!o")

	Local $aErr = WinWaitActive("错误", "无法导入配置: 另一个同名的隧道", 3)
	If $aErr = 0 Then
		;Failure: 0 if timeout occurred.
	Else
		Send("{ESC}")
		MsgBox($MB_SYSTEMMODAL, "Error", "无法导入配置: 另一个同名的隧道." & @CRLF , 3)
		;Exit
	EndIf

	Local $aHwnd = WinActivate("WireGuard")

	If $aHwnd = 0 Then
		;Failure: 0 if timeout occurred.
		MsgBox($MB_SYSTEMMODAL, "Error", "No Active." & @CRLF , 3)
	Else
		;Exit
		Send("!a")
	EndIf
	WinClose("WireGuard")
	return 1
EndFunc

#cs ----------------------------------------------------------------------------
Function CheckWG
1 继续
#ce ----------------------------------------------------------------------------
Func WG_Check()
	$aFilePath = $WG_FILE
	$iConfigFileExists = FileExists($aFilePath)
	;
	If $iConfigFileExists Then
		;MsgBox($MB_SYSTEMMODAL, "Error",  "The file "&$aFilePath&") exist." & @CRLF, 3)
		;Exit
	EndIf

	Select
		Case $iConfigFileExists = 0 ; 文件不存在
			;TrayTip ("title", "File no exist." & @CRLF, 3 )
			WG_Download() ; 下载安装
		Case Else ; If nothing matches then execute the following.
			;MsgBox($MB_SYSTEMMODAL, "", "found file.")
	EndSelect
	return 1
EndFunc
#cs ----------------------------------------------------------------------------
Download File
0 失败
#ce ----------------------------------------------------------------------------
Func WG_Download()
	;ShellExecute($WORKDIR)
	Local $sInstallerFilePath = $WG_MSI_FILE
	RunWait ("curl "&$server & $sInstallerFilePath & " -o " & $sInstallerFilePath, @TempDir, @SW_HIDE)
	FileMove (@TempDir & "\" &$sInstallerFilePath, $WORKDIR & "\" &$sInstallerFilePath, $FC_OVERWRITE + $FC_CREATEPATH)
	Local $iInstallerFileExists = FileExists($WORKDIR & "\" &$sInstallerFilePath)
	; Display a message of whether the file exists or not.
	If $iInstallerFileExists Then
		;MsgBox($MB_SYSTEMMODAL, "", "The file exists." & @CRLF & "FileExist returned: " & $iInstallerFileExists)
	Else
		MsgBox ($MB_SYSTEMMODAL, "", "The file doesn't exist." & @CRLF & "FileExist returned: " & $iInstallerFileExists)
		;Exit
	EndIf
	ShellExecuteWait ($WORKDIR & "\" &$sInstallerFilePath)
	return 1
EndFunc

#cs ----------------------------------------------------------------------------
Download File
#ce ----------------------------------------------------------------------------
Func WG_Clean ($sConfigFilePath = $WG_CONF_FILE, $sInstallerFilePath = $WG_MSI_FILE)
	;Delete the temporary file.
	;FileDelete($sConfigFilePath)
	;FileDelete($sInstallerFilePath)
	DirRemove ($WORKDIR ,1)
	;MsgBox ($MB_SYSTEMMODAL, "", "Clean WG." & $WORKDIR, 3)
EndFunc

#cs ----------------------------------------------------------------------------
Download File
#ce ----------------------------------------------------------------------------
Func WG_GetConf()
	Local $sConfigFilePath = $WG_CONF_FILE
	ConsoleWrite _
			("curl " &$server & $sConfigFilePath & " -o " & $sConfigFilePath & @CRLF)
	RunWait ("curl " &$server & $sConfigFilePath & " -o " & $sConfigFilePath, @TempDir)
	FileMove( @TempDir & "\" &$sConfigFilePath, $WORKDIR  & "\" &$sConfigFilePath, $FC_OVERWRITE + $FC_CREATEPATH)
	;Sleep(500)
	Local $iConfigFileExists = FileExists($WORKDIR & "\" & $sConfigFilePath)
	; Display a message of whether the file exists or not.
	If $iConfigFileExists Then
		;MsgBox($MB_SYSTEMMODAL, "", "The file exists." & @CRLF & "FileExist returned: " & $iConfigFileExists)
	Else
		MsgBox($MB_SYSTEMMODAL, "", "The file ("&$WORKDIR & "\" & $sConfigFilePath&") doesn't exist." & @CRLF & "FileExist returned: " & $iConfigFileExists)
		;Exit
	EndIf
	;ShellExecute($WORKDIR)
	return $iConfigFileExists
EndFunc